package com.yohann.algorithm;

/**
 * <p>
 * 二叉树演示
 * </p>
 *
 * @author Yohann
 * @since 2021/1/3 12:01
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        //创建节点
        HeroNode node1 = new HeroNode(1, "松江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");

        //创建二叉树
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.setRoot(node1);
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);

        System.out.println("<=========================>");
        System.out.println("前序遍历：");
        binaryTree.preOrder();
        System.out.println("<=========================>");
//        System.out.println("中序遍历：");
//        binaryTree.midOrder();
//        System.out.println("<=========================>");
//        System.out.println("后序遍历：");
//        binaryTree.postOrder();
//        System.out.println("<=========================>");
//        binaryTree.preorderSearch(5);
//        System.out.println("<=========================>");
//        binaryTree.midSearch(4);
//        System.out.println("<=========================>");
//        binaryTree.postSearch(6);
//        System.out.println("<=========================>");

        binaryTree.delNode(4);
        System.out.println("<=========================>");
        System.out.println("前序遍历：");
        binaryTree.preOrder();
        System.out.println("<=========================>");
    }
}

/**
 * 二叉树
 */
class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //前序遍历
    public void preOrder() {
        if (root != null) {
            root.preOrder();
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //中序遍历
    public void midOrder() {
        if (root != null) {
            root.midOrder();
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //后序遍历
    public void postOrder() {
        if (root != null) {
            root.postOrder();
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //前序遍历查找
    public void preorderSearch(int no) {
        if (root != null) {
            HeroNode result = root.preorderSearch(no);

            if (result == null) {
                System.out.println("没有目标节点！");
            } else {
                System.out.println(result);
            }
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //中序遍历查找
    public void midSearch(int no) {
        if (root != null) {
            HeroNode result = root.midSearch(no);

            if (result == null) {
                System.out.println("没有目标节点！");
            } else {
                System.out.println(result);
            }
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //后序遍历查找
    public void postSearch(int no) {
        if (root != null) {
            HeroNode result = root.postSearch(no);

            if (result == null) {
                System.out.println("没有目标节点！");
            } else {
                System.out.println(result);
            }
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //删除节点
    public void delNode(int no) {
        if (root != null) {
            //判断根节点是否是目标节点
            if (root.getNo() == no) {
                root = null;
            } else {
                root.delNode(no);
            }
        } else {
            System.out.println("当前树为空");
        }
    }
}

/**
 * 节点
 */
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //前序遍历
    public void preOrder() {
        /*
        1.输出当前节点
        2.左子节点不为空？递归
        3.右子节点不为空？递归
         */

        //输出父节点
        System.out.println(this);

        //向左子树递归
        if (this.left != null) {
            this.left.preOrder();
        }

        //向右子树递归
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    //中序遍历
    public void midOrder() {
        /*
        1.向左递归
        2.输出当前节点
        3.向右递归
         */

        //向左子树递归
        if (this.left != null) {
            this.left.midOrder();
        }

        //输出当前点
        System.out.println(this);

        //向右子树递归
        if (this.right != null) {
            this.right.midOrder();
        }
    }

    //后序遍历
    public void postOrder() {
        /*
        1.向左递归
        2.向右递归
        3.输出当前节点
         */

        //向左子树递归
        if (this.left != null) {
            this.left.postOrder();
        }

        //向右子树递归
        if (this.right != null) {
            this.right.postOrder();
        }

        //输出当前点
        System.out.println(this);
    }

    //前序遍历查找
    public HeroNode preorderSearch(int no) {
        /*
        1.当前节点是否符合
        2.左子节点不为空？递归
        3.右子节点不为空？递归
         */

        //判断当前节点是否符合
        if (this.no == no) {
            return this;
        }

        //向左子树递归
        if (this.left != null) {
            HeroNode result = this.left.preorderSearch(no);

            if (result != null) {
                return result;
            }
        }

        //向右子树递归
        if (this.right != null) {
            return this.right.preorderSearch(no);
        }

        return null;
    }

    //中序遍历查找
    public HeroNode midSearch(int no) {

        /*
        1.当前节点是否符合
        2.左子节点不为空？递归
        3.右子节点不为空？递归
         */

        //向左子树递归
        if (this.left != null) {
            HeroNode result = this.left.midSearch(no);

            if (result != null) {
                return result;
            }
        }

        //判断当前节点是否符合
        if (this.no == no) {
            return this;
        }

        //向右子树递归
        if (this.right != null) {
            return this.right.midSearch(no);
        }

        return null;
    }

    //后序遍历查找
    public HeroNode postSearch(int no) {

        /*
        1.当前节点是否符合
        2.左子节点不为空？递归
        3.右子节点不为空？递归
         */
        //向左子树递归
        if (this.left != null) {
            HeroNode result = this.left.postSearch(no);

            if (result != null) {
                return result;
            }
        }

        //向右子树递归
        if (this.right != null) {
            HeroNode result = this.right.postSearch(no);

            if (result != null) {
                return result;
            }
        }

        //判断当前节点是否符合
        if (this.no == no) {
            return this;
        }

        return null;
    }

    //删除节点
    public void delNode(int no) {
        //当前节点的左节点为目标节点
        if (this.left != null) {
            if (this.left.no == no) {
                this.left = null;
                return;
            }

            //左子树递归删除
            this.left.delNode(no);
        }

        //当前节点的右节点为目标节点
        if (this.right != null) {
            if (this.right.no == no) {
                this.right = null;
                return;
            }

            //右子树递归删除
            this.right.delNode(no);
        }
    }
}